package com.grass.cropTradingCenter.domain;

import com.grass.cropTradingCenter.domain.repository.CropRepository;
import com.grass.cropTradingCenter.domain.repository.GoodsPackageRepository;
import com.grass.cropTradingCenter.domain.repository.UserStarRepository;
import com.grass.cropTradingCenter.mapper.MiniShopMapper;
import com.grass.cropTradingCenter.pojo.dto.CropDto;
import com.grass.cropTradingCenter.pojo.dto.StartCropDto;
import com.grass.cropTradingCenter.pojo.entity.Goods;
import com.grass.cropTradingCenter.pojo.entity.GoodsPackage;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.entity.UserStar;
import com.grass.cropTradingCenter.pojo.po.QueryImage;
import com.grass.cropTradingCenter.pojo.po.StarType;
import com.grass.cropTradingCenter.pojo.vo.user.CropSearchVo;
import com.grass.cropTradingCenter.service.ImageService;
import com.grass.cropTradingCenter.service.UserCommitService;
import com.grass.cropTradingCenter.service.domain.CropDomainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CropDomainServiceImpl implements CropDomainService {

    @Autowired
    CropRepository cropRepository;

    @Autowired
    GoodsPackageRepository goodsPackageRepository;

    @Autowired
    ImageService imageService;

    @Autowired
    MiniShopMapper miniShopMapper;

    @Autowired
    UserStarRepository userStarRepository;

    @Autowired
    UserCommitService userCommitService;

    @Override
    public List<CropDto> getGoodsList(CropSearchVo vo) {
        List<Goods> goodsList = cropRepository.getGoodsList(vo);
        return covertCrop(goodsList);
    }


    @Override
    public CropDto cropDetail(int id) {
        Goods goods = cropRepository.cropDetail(id, true);
        CropDto cropDto = covertCrop(Collections.singletonList(goods)).get(0);
        /**
         * 是否收藏
         */
        cropDto.setIsStar(userStarRepository.isStart(new StarType(id, 1)));
        // 好评率
        cropDto.setGoodRate(userCommitService.getGoodEvaluateNum(id));
        return cropDto;
    }

    @Override
    public void checkStarStatus(StarType vo) {
        userStarRepository.checkStarStatus(vo);
        int num = vo.getIsStar() ? 1 : -1;
        cropRepository.updateStarNum(vo.getOtherId(), num);
    }

    @Override
    public List<StartCropDto> getStarList() {
        List<UserStar> starList = userStarRepository.getStarByUserAndType(1);
        return starList.stream().map(userStar -> {
            StartCropDto dto = new StartCropDto();
            CropDto goods = cropDetailDotAddBrowse(userStar.getOtherId());
            dto.setDisplayUrl(goods.getDisplayUrl());
            dto.setCreateTime(userStar.getCreateTime());
            dto.setGoodsDescription(goods.getGoodsDescription());
            dto.setMinPrice(goods.getMinPrice());
            dto.setGoodsName(goods.getGoodsName());
            dto.setGoodsId(goods.getGoodsId());
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public CropDto cropDetailDotAddBrowse(int id) {
        Goods goods = cropRepository.cropDetail(id, false);
        CropDto cropDto = covertCrop(Collections.singletonList(goods)).get(0);
        /**
         * 是否收藏
         */
        cropDto.setIsStar(userStarRepository.isStart(new StarType(id, 1)));
        return cropDto;
    }

    @Override
    public List<CropDto> getCropByShopId(int shopId) {
        List<Goods> goodsList = cropRepository.getGoodsListByShopId(shopId);
        return covertCrop(goodsList);
    }

    private List<CropDto> covertCrop(List<Goods> goodsList) {
        return goodsList.stream().map(goods -> {
            CropDto cropDto = new CropDto();
            BeanUtils.copyProperties(goods, cropDto);
            List<String> imageList = imageService.getImageList(new QueryImage(goods.getGoodsId(), 1));
            cropDto.setDetailImages(imageList);

            List<GoodsPackage> packages = goodsPackageRepository.getGoodsPackageByGoodsId(goods.getGoodsId());

            BigDecimal minPrice = new BigDecimal("9999");
            for (GoodsPackage aPackage : packages) {
                minPrice = minPrice.min(aPackage.getPrice());
            }
            cropDto.setPackages(packages);
            cropDto.setMinPrice(minPrice);

            MiniShop miniShop = miniShopMapper.selectById(goods.getShopId());
            cropDto.setShopName(miniShop.getShopName());
            return cropDto;
        }).collect(Collectors.toList());
    }
}
